#include<iostream>
#include<cmath>
#include<iomanip>
#define  PI 3.1415926 //假定PI值
using namespace std;

//用来精确7位小数
double p = 1e-7;

//二分法近似的方程表达式
double func(double x){
	return x * x * x - 3 * x - 1;
}
//二分法近似
void Dichotomy() {
	double max = 3;
	double min = 1;
	double mid = (max + min) / 2;
	if (func(mid) != 0) {
		while (abs(func(mid)) > p) {
			if (func(mid) * func(min) > 0) {
				min = mid;
			}
			else {
				max = mid;
			}
			mid = (max + min) / 2;
		}
	}
	cout << "二分法近似结果：" << setprecision(8) << mid << endl;
}

//牛顿法近似
void Newton() {
		double appro, actual, iter, destination = 4;
		do {
			iter = destination;
			appro = 3 * iter * iter - 3;//近似方程
			actual = iter * iter * iter - 3 * iter - 1;//实际方程
			destination = iter - actual / appro;//用最终结果去减两个方程的商，逐渐迭代
		} while (abs(iter - destination) > p);
		cout << "牛顿法近似结果：" << setprecision(8) << destination << endl;
}

int main() {
		cout<< "2cos20 " << " = " << setprecision(8) << 2 * cos(PI / 9) << endl;//注意cos的传入值是弧度制
		Dichotomy();
		Newton();
		return 0;
	}
